package club.zhilin.guli.service.edu.controller.portal;


import club.zhilin.guli.common.base.domain.JwtInfo;
import club.zhilin.guli.common.base.result.CommonResult;
import club.zhilin.guli.common.base.util.JwtUtil;
import club.zhilin.guli.service.edu.domain.CourseCollect;
import club.zhilin.guli.service.edu.domain.vo.CourseCollectVO;
import club.zhilin.guli.service.edu.service.ICourseCollectService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 * 课程收藏 前端控制器
 * </p>
 *
 * @author zhilin
 * @since 2022-01-11 15:29:01
 */
@RestController
@RequestMapping("/portal/edu/courseCollect")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class PortalCourseCollectController {

    private final ICourseCollectService courseCollectService;

    @ApiOperation("收藏课程")
    @PostMapping("/auth/collect/{courseId}")
    public CommonResult<String> collect(@PathVariable String courseId, @ApiIgnore HttpServletRequest request) {
        CommonResult<JwtInfo> jwtInfoCommonResult = JwtUtil.requestGetMember(request);
        if (jwtInfoCommonResult.getData() == null) {
            return CommonResult.failed("请先进行登录");
        }
        return courseCollectService.collect(courseId, jwtInfoCommonResult.getData().getId());
    }

    @ApiOperation("删除收藏")
    @DeleteMapping("/auth/collect/{courseId}")
    public CommonResult<String> delCollect(@PathVariable String courseId, @ApiIgnore HttpServletRequest request) {
        CommonResult<JwtInfo> jwtInfoCommonResult = JwtUtil.requestGetMember(request);
        if (jwtInfoCommonResult.getData() == null) {
            return CommonResult.failed("请先进行登录");
        }
        return courseCollectService.delCollect(courseId, jwtInfoCommonResult.getData().getId());
    }

    @ApiOperation("当前用户收藏列表")
    @GetMapping("/auth/collect/list")
    public CommonResult<List<CourseCollectVO>> list(@ApiIgnore HttpServletRequest request) {
        CommonResult<JwtInfo> jwtInfoCommonResult = JwtUtil.requestGetMember(request);
        if (jwtInfoCommonResult.getData() == null) {
            return CommonResult.failed("请先进行登录");
        }
        return courseCollectService.selectList(jwtInfoCommonResult.getData().getId());
    }

    @ApiOperation("判断是否收藏")
    @GetMapping("/auth/collect/hasCollect/{courseId}")
    public CommonResult<Boolean> hasCollect(@PathVariable String courseId, HttpServletRequest request) {
        CommonResult<JwtInfo> jwtInfoCommonResult = JwtUtil.requestGetMember(request);
        if (jwtInfoCommonResult.getData() == null) {
            return CommonResult.failed("请先进行登录");
        }
        long count = courseCollectService.count(new LambdaQueryWrapper<CourseCollect>().eq(CourseCollect::getCourseId, courseId).eq(CourseCollect::getMemberId, jwtInfoCommonResult.getData().getId()));
        return CommonResult.success(count > 0);
    }

}

